package com.example.demo6.ui.center;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo6.R;
import com.example.demo6.SQLite.LostGoods;
import com.example.demo6.ui.lose.LostAdapter;

import java.util.List;


public class MylostAdapter  extends RecyclerView.Adapter<MylostAdapter.ViewHolder> {

    private List<LostGoods> mData;
    private LayoutInflater layoutInflater;
    Context context;

    public MylostAdapter(Context context, List<LostGoods> mData) {
        this.context=context;
        layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mData = mData;
    }

    public  class ViewHolder extends RecyclerView.ViewHolder {
        public TextView ShowMyLostID;
        public TextView ShowMyLostTime;

        public TextView ShowMyLostItem;
        public TextView ShowMyLostRegion;

        public ViewHolder(View convertView) {
            super(convertView);
            ShowMyLostID=convertView.findViewById(R.id.ShowMyLostID);
            ShowMyLostTime=convertView.findViewById(R.id.ShowMyLostTime);
            ShowMyLostItem = convertView.findViewById(R.id.ShowMyLostItem);
            ShowMyLostRegion =convertView.findViewById(R.id.ShowMyLostRegion);
        }
    }
    @NonNull
    @Override
    public MylostAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v= layoutInflater.inflate(R.layout.fragment_myloselist,parent, false);
        return new MylostAdapter.ViewHolder(v);
    }
    @Override
    public void onBindViewHolder(@NonNull MylostAdapter.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        //使用Room，有变化
//            holder.ShowLostID.setText(mData.get(position).goodsId);
        holder.ShowMyLostID.setText(Integer.toString(mData.get(position).goodsId));
        holder.ShowMyLostTime.setText(mData.get(position).lostTime);
        holder.ShowMyLostItem.setText(mData.get(position).lostGoods);
        holder.ShowMyLostRegion.setText(mData.get(position).lostPlace);

        //导航到detailfragment
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController= Navigation.findNavController((Activity) context,R.id.nav_host_fragment_activity_main);
                Bundle bundle=new Bundle();
                bundle.putInt("goodsId",mData.get(position).goodsId);
                navController.navigate(R.id.edit_mylostfragment,bundle);
            }
        });
    }
    @Override
    public int getItemCount() {
        return mData.size();
    }
}
